<?php

require_once dirname(__FILE__) . '/../common/IController.php';
require_once dirname(__FILE__) . '/../common/BaseController.php';

class AdminEventController extends BaseController implements IController {
    
	public function __construct(Database $database, FBLoginModel $loginModel, NavigationView $navigationView) {
	    parent::__construct($database, $loginModel, $navigationView);
	}

	/**
	* Låter administratörer hantera event
	*
	* @return String HTML
	*/
	public function doControl() {		
		$html = '';

		if ($this->loginModel->getUserRole() & UsersModel::ADMIN_ROLE) {
			$em = new EventModel($this->database);
			$ev = new EventView();

			if ($ev->triedToDeleteEvent()) {
				$visitorEventId = $ev->getEventId();
				$em->deleteEvent($visitorEventId);
			}
			if ($ev->triedToEditEvent()) {
				$visitorEventId = $ev->getEventId();
				$eventPair = $em->getEvent($visitorEventId);	
				$html .= $ev->getEventForm($eventPair);
			} else {
				$html .= $ev->getEventForm();
			}
			if ($ev->triedToSubmitEvent()) {
				$event = $ev->getEventInput();
				$em->addEvent($event);
			}		

			$events = $em->getEvents();
			$html .= $ev->getAdminEventsMarkup($events);
		} else {
			$html .= 'inte auktoriserad';
		}

		return $html;
	}
}